<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="
        isNewRef ? modalConfig.header.newTitle : modalConfig.header.editTitle
      "
      width="30%"
      center
    >
      <div class="form">
        <el-form v-model="formData" label-width="100px">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item :label="item.label" :prop="item.prop">
              <!-- <component :is="`el-+${item.type}`"></component> -->
              <template v-if="item.type === 'input'">
                <el-input
                  v-model="formData[item.prop]"
                  :placeholder="item.palceholder"
                />
              </template>
              <!-- 日期 -->
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="formData[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
              <!-- select -->
              <template v-if="item.type === 'select'">
                <el-select
                  v-model="formData[item.prop]"
                  :placeholder="item.palceholder"
                  style="width: 100%"
                >
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value" />
                  </template>
                </el-select>
              </template>
              <!-- 自定义组件插槽 -->
               <template v-if="item.type==='custom'">
                <slot :name="item.slotName"></slot>
               </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <!-- 确定/取消 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelClick">取消</el-button>
          <el-button type="primary" @click="handleConfirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { storeToRefs } from "pinia"
import useMainStore from "@/stores/main/main"
import useSystemStore from "@/stores/main/system/system"
import { reactive, ref } from "vue"
// 定义props
interface IProps {
  modalConfig: {
    pageName: string
    header: {
      newTitle: string
      editTitle: string
    }
    formItems: any[]
  },
  otherInfo?:any
}
const props = defineProps<IProps>()

const dialogVisible = ref(false)
const mainStore = useMainStore()
const systemStore = useSystemStore()
// 判断编辑不需要密码
const isNewRef = ref<boolean>()
// 记录存储编辑用户数据
const editData = ref()
// mainStore.fetchEntireDataAction()
// 获取角色列表
const { entireDepartments } = storeToRefs(mainStore)
// 定义内部数据
const initialData: any = {}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = item.initialValue ?? ""
}
const formData = reactive<any>(initialData)

//
function setModalVisible(isNew: boolean, itemData?: any) {
  dialogVisible.value = true
  isNewRef.value = isNew
  if (!isNew && itemData) {
    // console.log("哈哈哈")
    for (const key in itemData) {
      // if(key==="createAt"||key==="updateAt"){
      //   console.log(formatUTC(itemData[key]),"哈哈哈哈")
      //   formDate[key] = formatUTC(itemData[key])
      // }
      formData[key] = itemData[key]
    }
    editData.value = itemData
  } else {
    for (const key in formData) {
      // const item=modalCofig.formItems.find((item)=>item.prop===key)
      // formData[key] =item?(item?.initialValue):" "
      formData[key]=""
    }
    // console.log(formData)
    editData.value = null
  }
}
// 确定添加数据
function handleConfirm() {
  dialogVisible.value = false
  // console.log(editData.value)
  // console.log(isNewRef)
  // 编辑部门
let infoData=formData
if(props.otherInfo){
  infoData={...infoData,...props.otherInfo}
}
  if (!isNewRef.value && editData.value) {
    console.log("编辑")
    // console.log(formData)
    systemStore.editPageDataDataAction(
     props.modalConfig.pageName,
      editData.value.id,
      infoData
    )
  } else {
    // 新建部门
    console.log("添加")
    // console.log(formData)
    systemStore.newPageDataAction(props.modalConfig.pageName, infoData)
  }
}
// 取消按钮事件
function cancelClick() {
  dialogVisible.value = false
}

defineExpose({
  setModalVisible,
  handleConfirm
})
</script>
<style scoped lang="less">
.modal {
}
.form {
  padding: 0 20px;
}
</style>
